//
//  AddConnact1ViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/30.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import SnapKit

class AddConnact1ViewController: BaseUIViewController {

    let nfcUnit = NFCUint()
    let locationUnit = LocationUnit()
    
    let locationLabel = UILabel()
    let textField = AddConnactTextField()
    
    var editedAddress: String? = nil
    var editedLongitude: String? = nil
    var editedLatitude: String? = nil
    
    var editModel: ConnactModel?
    
    init(editModel: ConnactModel? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.editModel = editModel
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        self.navigationItem.title = "售后服务".localized
        
        if editModel == nil {
            locationUnit.startLocation()
            locationUnit.addressUpdate = { [weak self] address in
                if self?.editedAddress == nil {
                    self?.locationLabel.text = address
                }
            }
        }
        
        let currentAddress = editModel?.address ?? ""
        locationLabel.config
            .text(currentAddress.count > 0 ? currentAddress : "暂未获取到定位信息".localized)
            .textColor(#colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1))
            .font(.systemFont(ofSize: 12, weight: .medium))
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(35)
                make.top.equalTo(20)
                make.right.lessThanOrEqualTo(-50)
        }
        
        UIButton().config
            .title("修改".localized)
            .titleColor(#colorLiteral(red: 0.2117647059, green: 0.6, blue: 1, alpha: 1))
            .font(.systemFont(ofSize: 12, weight: .medium))
            .addTarget(self, action: #selector(editAddress))
            .isHidden(editModel != nil)
            .addTo(view)
            .makeConstraints { (make) in
                make.centerY.equalTo(locationLabel.snp.centerY)
                make.width.equalTo(30)
                make.left.equalTo(locationLabel.snp.right).offset(5)
        }
                
        UIImageView().config
            .contentMode(.scaleAspectFit)
            .image(AppIcon.image("", size: 15, color: APPConfig.baseColor))
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(15)
                make.height.width.equalTo(17)
                make.centerY.equalTo(locationLabel.snp.centerY)
        }
        
        textField.config
            .text(editModel?.number ?? "")
            .addTo(view)
            .extra {
                $0.isEditable = false
                $0.isUserInteractionEnabled = false
                $0.placeHolder = "点击按钮获取序列号".localized
                $0.textContainerInset = .init(top: 15, left: 0, bottom: 15, right: 90)
        }.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(locationLabel.snp.bottom).offset(20)
        }
        
        UIButton().config
            .isHidden(editModel != nil)
            .title("  " + "获取序列号".localized + "  ")
            .titleColor(.white)
            .fontSize(14)
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .cornerRadius(5)
            .clipsToBounds(true)
            .addTarget(self, action: #selector(scan))
            .addTo(view)
            .makeConstraints { (make) in
                make.right.equalTo(-15)
                make.centerY.equalTo(textField.snp.centerY)
                make.height.equalTo(28)
        }
        
        UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .title("下一步".localized)
            .cornerRadius(5)
            .clipsToBounds(true)
            .titleColor(.white)
            .addTarget(self, action: #selector(nextStep))
            .addTo(view)
            .makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.left.equalTo(40)
                make.height.equalTo(50)
                make.top.equalTo(textField.snp.bottom).offset(65)
        }
    }
    
    @objc func editAddress() {
        let vc = ChangeAddressViewController.init(currentAddress: locationUnit.addressLine)
        vc.editAddressBlock = { [weak self] address in
            self?.editedAddress = address
            self?.locationLabel.text = address
            self?.locationUnit.geocodeLocation(address, success: { (longitude, latitude) in
                self?.editedLongitude = longitude
                self?.editedLatitude = latitude
            })
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func scan() {
        let vc = ScanVerifyViewController.init(longitude: locationUnit.longitude, latitude: locationUnit.latitude, address: locationUnit.addressLine)
        vc.scanComplete = { [weak self] code in
            self?.textField.text = code
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func nextStep() {
        guard let text = self.textField.text,
            text.count > 0 else {
                APPHud.shared.showError(status: "点击按钮获取序列号".localized)
                return
        }
        var model = AddConcatModel()
        model.number = text
        model.latitude = locationUnit.latitude
        model.longitude = locationUnit.longitude
        model.address = locationUnit.addressLine
        if let editedAddress = self.editedAddress {
            model.latitude = editedLatitude ?? ""
            model.longitude = editedLongitude ?? ""
            model.address = editedAddress
        }
        self.navigationController?
            .pushViewController(AddConnact2ViewController(editModel: editModel, model: model), animated: true)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
    
}
